package properties;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * @author liufei
 * @date 2021/8/9 10:47
 */
public class BatchBGProperties extends BaseProperties {

    private List<User> users = Collections.emptyList();

    public BatchBGProperties() {
        init(readProperties("batch-bg.properties"));
    }

    private static final int NAME_INDEX = 0;
    private static final int PWD_INDEX = 1;
    /**
     * 报工类型
     */
    private static final int TYPE_INDEX = 2;
    private static final int PROJECT_NAME_INDEX = 3;
    private static final int WORK_TYPE_INDEX = 4;
    private static final int EMAIL_INDEX = 5;
    private static final Integer[] indexs = new Integer[]{NAME_INDEX, PWD_INDEX, TYPE_INDEX, PROJECT_NAME_INDEX, WORK_TYPE_INDEX, EMAIL_INDEX};

    private void init(Properties properties) {
        users = new ArrayList<>();
        for (String userProperties : properties.stringPropertyNames()) {
            String[] split = properties.getProperty(userProperties).split(",");
            String name = split[NAME_INDEX];
            String pwd = split[PWD_INDEX];
            String reportForWorkType = split[TYPE_INDEX];
            String projectName = split[PROJECT_NAME_INDEX];
            String workType = split[WORK_TYPE_INDEX];
            String email = null;
            if (split.length == indexs.length) {
                email = split[EMAIL_INDEX];
            }
            users.add(new User(name, pwd, reportForWorkType, projectName, workType, email));
        }
        logger.info("加载配置文件");
        logger.info(this.toString());
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    @Override
    public String toString() {

        return "properties.AutoQDProperties{" +
                "users=" + users +
                '}';
    }

    public static void main(String[] args) {
        BatchBGProperties autoQDProperties = new BatchBGProperties();
        System.out.println(autoQDProperties);
    }
}
